﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Use this filter to smooth a picture.
    /// A classical Matrix operation is used to achieve this
    /// </summary>
    public class Smooth : BaseImageFilter
    {
        protected int m_nWeight = 1;
		CConvMatrix matrix = new CConvMatrix();

        /// <summary>
        /// Initializes a new instance of the <see cref="Smooth"/> class.
        /// </summary>
        public Smooth()
        {
            matrix.SetAll(1);
            matrix.Pixel = m_nWeight;
            matrix.Factor = m_nWeight + 8;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Smooth"/> class.
        /// </summary>
        /// <param name="_nWeight">The _n weight.</param>
        public Smooth(int _nWeight)
            : this()
        {
            m_nWeight = _nWeight;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Smooth"/> class.
        /// </summary>
        /// <param name="_smooth">The _smooth.</param>
        internal Smooth(Smooth _smooth)
        {
            m_nWeight = _smooth.m_nWeight;
        }

        /// <summary>
        /// Gets or sets the weight.
        /// </summary>
        /// <value>The weight.</value>
        public int Weight
        {
            get { return m_nWeight; }
            set { m_nWeight = value; }
        }

        /// <summary>
        /// Smoothes the specified bitmap.
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
			matrix.Pixel = m_nWeight;
			matrix.Factor = m_nWeight + 8;

			new Convolution3x3(matrix).Execute(_bitmap);
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Smooth(this);
        }
    }
}
